package org.ediba.siri.web.rest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ediba.siri.core.user.domain.UserRole;
import org.ediba.siri.core.user.service.UserService;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.ResponseBody;

public class UserRoleController extends Controller {
	
	private UserService userService;
	public void setUserService(UserService userService) {this.userService = userService;}
	
	@ResponseBody
	public Map<String,Object> getHandler(HttpServletRequest request, HttpServletResponse response, UserRoleCommand command){
		boolean effective = ServletRequestUtils.getBooleanParameter(request,"effective",false);
		
		List<UserRole> roles = null;
		if ((command.getIdsUser() != null)&&(effective))	//pido roles para un usuario (pido permisos efectivos)
			roles = this.userService.roleRetrieveForUser(command.getIdsUser()[0]);
		else if ((command.getIdsUser() != null)&&(!effective)) //pido los roles para el usuario
			roles = this.userService.roleRetrieveByUser(command.getIdsUser()[0]);
		else if (command.getIdsUserGroup() != null) //pido los roles para el grupo
			roles = this.userService.roleRetrieveByUserGroup(command.getIdsUserGroup()[0]);
		else //pido todos los roles			
			roles = this.userService.roleRetrieve();
		
		Map<String,Object> _result = new HashMap<String,Object>(4);
		_result.put("data", roles);
		_result.put("total", roles.size());
		_result.put("success", true);
		_result.put("message", "OK");
		return _result;
	}
}
